<?php

/**
 * XHTML_Button, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Button class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_All_Attributes_Element.php";
include_once "XHTML__Inline_Forms.php";
include_once "XHTML__Button_Content.php";

/**
 * The XHTML_Button class is a handler for the input xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Button extends XHTML_All_Attributes_Element 
    implements XHTML__Inline_Forms {
    
    // xhtml attributes
    
    /**
     * Specifies a unique name for the button.
     * 
     * @var string
     */
    protected $name;
    
    /**
     * Specifies an initial value for the button. The value can be changed by 
     * a script.
     *
     * @var string
     */
    protected $value;
    
    /**
     * Defines the type of button.
     * Valid values: button, submit, reset.
     *
     * @var string
     */
    protected $type;
    
    /**
     * Disables the button.
     *
     * @var boolean
     */
    protected $disabled;
    
    /**
     * Array of button tag's inner elements. (XHTML__Button_Content)
     *
     * @var array
     */
    protected $elements;
    
    
    // Methods
    
    /**
     * Constructs a XHTML_Input object.
     */
    function __construct()
    {
        settype($this->elements, "array");
    }
    
    /**
     * Sets the unique name for the button element.
     *
     * @param string $name
     */
    function setName($name)
    {
        $this->name = $name;
    }
    
    /**
     * Gets the unique name for the button element.
     *
     * @return string
     */
    function getName()
    {
        return $this->name;
    }
    
    /**
     * Sets an initial value for the button. The value can be changed by a 
     * script.
     * 
     * @param string $value
     */
    function setValue($value)
    {
        $this->value = $value;
    }
    
    /**
     * Gets an initial value for the button. The value can be changed by a 
     * script.
     *
     * @return string
     */
    function getValue()
    {
        return $this->value;
    }
    
    /**
     * Sets the type of the button element.
     * Valid values: button, submit, reset.
     * 
     * @param string $type
     */
    function setType($type)
    {
        $this->type = $type;
    }
    
    /**
     * Gets the type of the button element.
     * Valid values: button, submit, reset.
     *
     * @return string
     */
    function getType()
    {
        return $this->type;
    }
    
    /**
     * Sets the XHTML__Button_Content elements array.
     *
     * @param array $elements
     */
    function setElements($elements) {
        $this->elements = $elements;
    }
    
    /**
     * Gets the XHTML__Button_Content elements array.
     *
     * @return array
     */
    function getElements()
    {
        return $this->elements;
    }
    
    /**
     * Adds a XHTML__Button_Content element to the inner array of this object.
     * 
     * @param XHTML__Button_Content $element
     */
    function addElement(XHTML__Button_Content $element)
    {
        array_push($this->elements, $element);
    }
    
    /**
     * Removes a XHTML__Button_Content element from the inner array of this object.
     * 
     * @param XHTML__Button_Content $element
     */
    function removeElement(XHTML__Button_Content $element)
    {
        if (($res = array_search($element, $this->elements)) !== false) {
            unset($this->element[$res]);
        }
    }
    
    /**
     * Indicates if the the button element should be disabled when it first 
     * loads.
     * 
     * @param boolean $disabled
     */
    function setDisabled($disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Checks if the the button element should be disabled when it first 
     * loads.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<button".parent::toString();
        
        /*
         * Additions of the object own attributes to the return string.
         */
        
        if (isset($this->name)) {
            $auxString = $auxString."name=\"".$this->name."\" ";
        }
        
        if (isset($this->value)) {
            $auxString = $auxString."value=\"".$this->value."\" ";
        }
        
        if (isset($this->type)) {
            $auxString = $auxString."type=\"".$this->type."\" ";
        }
        
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Addition of the inner elements of the button tag.
         */
        if (count($this->elements) > 0) {
            foreach ($this->elements as $element) {
                $auxString = $auxString.$element->toString();
            }
        }
        
        $auxString = $auxString."</button>\n";
        
        return $auxString;
    }
    
}

?>